package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.OrderCancelForm;
import com.cskaoyan.gateway.form.shopping.OrderForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:
 * @description:
 * @keypoint:
 * @tags:
 * @related:
 */

@Slf4j
@RestController
@RequestMapping("shopping")
//@Anoymous //测试使用
public class OrderController {

    //    @Reference(check = false)
    @Reference(timeout = 3000, retries = 2, check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000, retries = 2, check = false)
//    @Reference(check = false)
    OrderQueryService orderQueryService;


    /**
     * 订单插入
     *
     * @param orderForm
     * @param request
     * @return
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody OrderForm orderForm, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        CreateOrderRequest createOrderRequest = new CreateOrderRequest();
        createOrderRequest.setUserId(uid);
//        createOrderRequest.setUserId(Long.valueOf(74)); //测试用

        createOrderRequest.setTel(orderForm.getTel());
        createOrderRequest.setUserName(orderForm.getUserName());
        createOrderRequest.setStreetName(orderForm.getStreetName());
        createOrderRequest.setCartProductDtoList(orderForm.getCartProductDtoList());
        createOrderRequest.setAddressId(orderForm.getAddressId());
        createOrderRequest.setOrderTotal(orderForm.getOrderTotal());

        CreateOrderResponse orderResponse = orderCoreService.createOrder(createOrderRequest);
        if (orderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(orderResponse.getOrderId(), orderResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(Integer.valueOf(orderResponse.getCode()), orderResponse.getMsg());

    }

    /**
     * 获取当前用户的所有订单
     *
     * @param request
     * @return
     */
    @GetMapping("order")
    public ResponseData getOrder(Integer page, Integer size, String sort, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
//        orderListRequest.setUserId(Long.valueOf(74)); //测试用
        orderListRequest.setPage(page);
        orderListRequest.setSize(size);
        orderListRequest.setSort(sort);

        OrderListResponse orderListResponse = orderQueryService.getOrderList(orderListRequest);
        OrderListVo orderListVo = new OrderListVo();
        orderListVo.setData(orderListResponse.getData());
        orderListVo.setTotal(orderListResponse.getTotal());
        if (orderListResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(orderListVo, orderListResponse.getMsg());
        }

        return new ResponseUtil().setErrorMsg(Integer.valueOf(orderListResponse.getCode()), orderListResponse.getMsg());

    }

    /**
     * 根据url上的id，获取订单详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("order/{id}")
    public ResponseData queryOrder(HttpServletRequest request, @PathVariable("id") String id) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
//
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);
        orderDetailRequest.setUserId(uid);
//        orderDetailRequest.setUserId(Long.valueOf(74)); //测试用

        OrderDetailResponse orderDetailResponse = orderQueryService.getOrderDetail(orderDetailRequest);

        if (orderDetailResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(orderDetailResponse.getOrderDetailDto(), orderDetailResponse.getMsg());
        }

        return new ResponseUtil().setErrorMsg(Integer.valueOf(orderDetailResponse.getCode()), orderDetailResponse.getMsg());
    }


    /**
     * 取消订单
     *
     * @param request
     * @return
     */
//    @PostMapping("order/{id}")
//    public ResponseData cancelOrder(HttpServletRequest request,@PathVariable("id") Long id){
//        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());
//
//        return null;
//    }
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody OrderCancelForm cancelForm, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(cancelForm.getOrderId());

        CancelOrderResponse cancelOrderResponse = orderQueryService.cancelOrder(cancelOrderRequest);

        if (cancelOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            ResponseUtil responseUtil = new ResponseUtil();
            ResponseData responseData = responseUtil.setData(cancelOrderResponse);
            return responseData;
//            return new ResponseUtil().setData(cancelOrderResponse,"success");
        }
        return new ResponseUtil().setErrorMsg(Integer.valueOf(cancelOrderResponse.getCode()), cancelOrderResponse.getMsg());
    }


    /**
     * 删除订单
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(HttpServletRequest request, @PathVariable("id") String id) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);

        DeleteOrderResponse deleteOrderResponse = orderQueryService.deleteOrder(deleteOrderRequest);
        if (deleteOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(deleteOrderResponse, deleteOrderResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(Integer.valueOf(deleteOrderResponse.getCode()), deleteOrderResponse.getMsg());
    }
}
